package com.sofoscorp.android.gestcobranzamobile;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class KNKK_BO {

	private KNKK_Str data;
	private KNKK_Str[] datas;
	private String path_FromSAP = "data/data/com.sofoscorp.android.initdb/databases/FromSAP";
	private String table_name = "KNKK";	
	private SQLiteDatabase db1;
	private String[] campos = new String[] {"KUNNR", "KKBER", "KLIMK", "SAUFT",
											"SKFOR", "SSOBL", "ZSAUF"};	
	
	public boolean do_conn() {
		try{
			db1 = SQLiteDatabase.openDatabase(path_FromSAP, null, SQLiteDatabase.OPEN_READWRITE);
		}catch(SQLiteException e){
			db1=null;
		}
		
		if(db1 != null){
			return true;
		}else{
			return false;
		}
		
	}		
	
	public KNKK_Str get_single_by_key(String kunnr, String kkber){
		//Obtener Limite de Credito para un Cliente en un Area Especifica
		Cursor c = null; 
		String where = "KUNNR = ? AND KKBER = ?";
		String[] whereArgs; 
		
		whereArgs = new String[]{kunnr, kkber};
		
		if(db1 != null){
			c = db1.query(table_name, campos, where, whereArgs, null, null, null);
		}		
		
		if (c.moveToFirst()){
			
			data = new KNKK_Str(c.getString(0), c.getString(1), c.getDouble(2),
					            c.getDouble(3), c.getDouble(4), c.getDouble(5),
					            c.getDouble(6));
			
		}else{
			data = null;
		}		
		
		return data;
	}
	
	public KNKK_Str[] get_all_kunnr(String kunnr){
		//Obtener Todos los Limites para un solo cliente
		
		
		Cursor c = null; 
		String where = "KUNNR=?";
		String[] whereArgs;  
		ArrayList<KNKK_Str> data_result = new ArrayList<KNKK_Str>();

		whereArgs = new String[]{kunnr};
		
		if(db1 != null){
			c = db1.query(table_name, campos, where, whereArgs, null, null, null);
		}
		
		if (c.moveToFirst()){
			
			do{
				
				data_result.add(new KNKK_Str(c.getString(0), c.getString(1), c.getDouble(2),
			                                 c.getDouble(3), c.getDouble(4), c.getDouble(5),
			                                 c.getDouble(6)));	
				
			}while(c.moveToNext());
			
		}else{
			data_result = null;
			datas = null;
			db1.close();
			return datas;
		}		
		
		datas = data_result.toArray(new KNKK_Str[0]);
		db1.close();
		return datas;
	}		
	
	//Nuevo Metodo para obtener clientes a partir de la sociedad
	//Agregado por Luis Silva		27.11.2013
	public KNKK_Str[] get_all(){
		//Obtener Todos los Limites para todos los Clientes
		
		Cursor c = null; 
		ArrayList<KNKK_Str> data_result = new ArrayList<KNKK_Str>();

		if(db1 != null){
			c = db1.query(table_name, campos, null, null, null, null, null);
		}
		
		if (c.moveToFirst()){
			
			do{
				
				data_result.add(new KNKK_Str(c.getString(0), c.getString(1), c.getDouble(2),
			                                 c.getDouble(3), c.getDouble(4), c.getDouble(5),
			                                 c.getDouble(6)));	
				
			}while(c.moveToNext());
			
		}else{
			data_result = null;
			datas = null;
			db1.close();
			return datas;
		}		
		
		datas = data_result.toArray(new KNKK_Str[0]);
		db1.close();
		return datas;
	}		
	
}
